/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.express100.callback.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.order.pojo.ShopOrders;
import com.hyyt.shopjsp.order.pojo.ShopShipping;
import com.hyyt.shopjsp.order.service.IShopOrdersService;
import com.hyyt.shopjsp.order.service.IShopShippingService;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.express100.bean.NoticeRequest;
import com.hyyt.shopjsp.express100.bean.NoticeResponse;
import com.hyyt.shopjsp.express100.bean.Result;
import com.hyyt.shopjsp.express100.callback.demo.JacksonHelper;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**快递100信息回调controller
 * @author: caiang
 * @Date 2016/8/13 11:09
 */
@Controller
@RequestMapping("/callBack/")
public class CallBackController extends BaseController {
    /**
     * SHOP_发货详情接口
     */
    @Resource
    private IShopShippingService shopShippingService;
    /**
     * SHOP_订单
     */
    @Resource
    private IShopOrdersService shopOrdersService;

    /**
     * 快递100信息回调
     * @param request
     * @return
     */
    @RequestMapping("backMessage")
    @ResponseBody
    public NoticeResponse backMessage(HttpServletRequest request){
        NoticeResponse resp = new NoticeResponse();
        resp.setResult(false);
        resp.setReturnCode("500");
        resp.setMessage("保存失败");
        String param = request.getParameter("param");
        try{
            NoticeRequest nReq = JacksonHelper.fromJSON(param,NoticeRequest.class);
            Result result = nReq.getLastResult();
            /**
             * 处理快递结果，在此进行保存操作，并且返回操作是否完成状态，根据状态判断是否返回结果。
             */
            Map params = new HashMap<>();
            params.put("deliverySn",result.getNu());
            params.put("code",result.getCom());
            ShopShipping shipping = shopShippingService.loadShopShipping(params);
            String state = result.getState();//取出物流当前状态
            Integer stateTemp = Integer.parseInt(state);
            if (Utils.objectIsNotEmpty(state)){
                /**
                 * 发货详情中的物流动态如果为已经签收时不改变状态
                 */
                if (Utils.objectIsNotEmpty(shipping)){
                    if(stateTemp==3){//当物流状态为已签收时改变订单状态为已完成
                        ShopOrders orders = shopOrdersService.loadShopOrders(shipping.getOrdersId());
                        orders.setOrdersState(5);//用户已确认收获
                        orders.setUpdateTime(new Timestamp(new Date().getTime()));//更新时间
                        shopOrdersService.updateShopOrders(orders);//更新订单
                    }
                    shipping.setState(stateTemp);//当前物流状态
                    shipping.setExpressInfo(FastJsonUtils.toJSONString(result));//物流信息
                    shopShippingService.updateShopShipping(shipping);//更新发货信息
                    resp.setResult(true);
                    resp.setReturnCode("200");
                    resp.setMessage("模块更新物流信息保存成功");
                }
            }else {
                resp.setMessage("物流无信息，保存失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            resp.setMessage("模块异常保存失败" + e.getMessage());
        }
        return resp;
    }
}
